package com.personal.cuncunxing.module.home.live;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.personal.cuncunxing.R;

/**
 * Description: 电视直播rv列表适配器
 * Created by kode on 2020/5/27.
 */
public class LiveRVAdapter extends RecyclerView.Adapter<LiveRVAdapter.ViewHolder> {

    private static final String TAG = "LiveRVAdapter";
    private static String[] title = {"央视", "地方", "卫视"};
    private LiveSubRVAdapter.OnTvPlay onTvPlay;
    private int lastSortPosition;//标记上一次的类别位置

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_live_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.live_title_tv.setText(title[position]);
        //设置适配器
        holder.liveSubRVAdapter = new LiveSubRVAdapter(position, this);
        holder.recycler_view.setAdapter(holder.liveSubRVAdapter);
        holder.liveSubRVAdapter.setOnTvPlay(onTvPlay);
    }

    public void updateUI() {
        notifyDataSetChanged();
    }

    public void updateLastItemUI(int position) {
        notifyItemChanged(position);
    }

    @Override
    public int getItemCount() {
        return title.length;
    }

    public void setOnTvPlay(LiveSubRVAdapter.OnTvPlay onTvPlay) {
        this.onTvPlay = onTvPlay;
    }

    public int getLastSortPosition() {
        return lastSortPosition;
    }

    public void setLastSortPosition(int lastSortPosition) {
        this.lastSortPosition = lastSortPosition;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView live_title_tv;
        LiveSubRVAdapter liveSubRVAdapter;
        private RecyclerView recycler_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            live_title_tv = itemView.findViewById(R.id.live_title_tv);
            recycler_view = itemView.findViewById(R.id.home_live_sub_rv);
            //设置流布局
            recycler_view.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        }
    }
}
